package com.book.bookshop.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.bookshop.entity.Book;
import com.book.bookshop.entity.BookVo;
import com.book.bookshop.mapper.BookMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class BookService extends ServiceImpl<BookMapper,Book>  {
    @Autowired
    private BookMapper bookMapper;

    public List<BookVo> serach(String name) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Book> books = bookMapper.selectList(queryWrapper);
        return handleList(books);
    }

    private List<BookVo> handleList(List<Book> books) {
        List<BookVo> bookVos = new ArrayList<>();

        for (int i = 0; i < books.size(); i++) {
            BookVo bookVo =  handle(books.get(i));
            bookVos.add(bookVo);
        }
        return bookVos;
    }

    private BookVo handle(Book book) {
        BookVo bookVo = new BookVo();
        BeanUtils.copyProperties(book,bookVo);
        bookVo.setId(book.getId().toString());
        return bookVo;
    }
}
